#include <iostream>

using namespace std;


struct ListNode{

    int value;
    ListNode * next;
};

// create LinkedList
ListNode * createNode(int value){
    ListNode * pHead = new ListNode();
    pHead->value = value;
    pHead -> next = nullptr;
    return pHead;
}

// connect ListNode
void connectNode( ListNode * node1 , ListNode * node2){
    node1 -> next = node2;
}

// delete specify node
ListNode * deleteNodeInList(ListNode * head, ListNode * node){
    
    ListNode * deleteNode;
    // delete first 
    if ( head == node ){
        deleteNode = head -> next;
        head -> next = deleteNode-> next;
        head -> value = deleteNode -> value;
        delete deleteNode;
    }else if ( node -> next == nullptr ) {
        // delete tail node
        ListNode * pHead = head;
        while ( pHead -> next -> next != nullptr )
            pHead = pHead -> next;
        pHead -> next = nullptr;
        deleteNode = node;
    }else {
        // delete middle node
        deleteNode = node -> next;
        head -> next = deleteNode-> next;
        head -> value = deleteNode -> value;
    }
    delete deleteNode;
    return head;
}

// print node info 
void printNodeInfo(ListNode * head ){
    while( head != nullptr ){
        cout << head -> value << endl;
        head = head -> next;
    }
}

int main(void){
    ListNode * node1 = createNode(2);
    ListNode * node2 = createNode(3);
    ListNode * node3 = createNode(4);
    ListNode * node4 = createNode(5);
    ListNode * node5 = createNode(6);
    
    connectNode(node1, node2);
    connectNode(node2, node3);
    connectNode(node3, node4);
    connectNode(node4, node5);

    // 删除指定节点
    ListNode * head = deleteNodeInList(node1, node1);

    printNodeInfo(head);
    return 0;
}
